import { getSignupProgress } from '/api/login.js'
import { bindWeChatUser, signInByWeChat } from '/api/login'

//app.js
App({
  onLaunch: function () {
    const _this = this
    // 转发
    wx.showShareMenu({
      withShareTicket: true
    })
    try {
      const value = wx.getStorageSync('token')
      if (value) {
        this.globalData.token = value
      } else {
        this.globalData.token = ''
      }
    } catch (e) {
      this.globalData.token = ''
    }

    wx.getSystemInfo({
      success: function(res) {
        _this.globalData.windowHeight = res.windowHeight
        _this.globalData.windowWidth = res.windowWidth
      }
    })

    if (this.globalData.token && this.globalData.token!=='') {
      this.getSignUpProgress()
    }

    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },

  getSignUpProgress() {
    getSignupProgress().then(res => {
      switch(res.signupProgress) {
        case 'CREATED':
          wx.showModal({
            title: '提示',
            content: '您尚未补充完整资料，请补充完整',
            success(r) {
              if (r.confirm) {
                wx.navigateTo({
                  url: '/pages/completeInfoFirst/completeInfoFirst'
                })
              }
            }
          })
          break;
        case 'BASE_INFO':
          wx.showModal({
            title: '提示',
            content: '您尚未补充完整资料，请补充完整',
            success(r) {
              if (r.confirm) {
                if ( res.identityType === 'STUDENT' ) {
                  wx.navigateTo({
                    url: '/pages/completeInfoStudent/completeInfoStudent'
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/completeInfoTeacher/completeInfoTeacher'
                  })
                }
              }
            }
          })
          break;
        default:
          break;
      }
    })
  },

  bindLogin() {
    const _this = this
    return new Promise((resolve, reject) => {
      _this.getLoginCode().then(code => {
        // 登录
        signInByWeChat({ code: code }).then(signInRes => {
          wx.setStorageSync('token', signInRes.token)
          _this.globalData.token = signInRes.token
          resolve(signInRes.token)
        }).catch(signInError => {
          _this.getLoginCode().then(loginCode => {
            // 注册
            bindWeChatUser({
              avatarUrl: wx.getStorageSync('userAvatar'),
              code: loginCode,
              nickname: wx.getStorageSync('userNickName')
            }).then(signUpRes => {
              wx.setStorageSync('token', signUpRes.token)
              _this.globalData.token = signUpRes.token
              resolve(signUpRes.token)
            }).catch(error => {
              reject(error)
            })
          }).catch(error => {
            reject(error)
          })
        })
      })
    })
  },

  getLoginCode () {
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          if (res.code) {
            resolve(res.code)
          } else {
            console.log('登录失败' + res.error)
            reject(res)
          }
        },
        fail(error) {
          console.log('登录失败' + error)
          reject(error)
        }
      })
    })
  },
  globalData: {
    windowWidth: 0,
    windowHeight: 0,
    navHeight: 0,
    token: ''
  }
})
